package com.esreport.api;

import com.esreport.Utils.ResultJson;
import com.esreport.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopApi {
    @Autowired
    private ReportService reportService;

    @RequestMapping("/countAction")
    public Object countAction(Integer shopId,String action) {
        long count = reportService.countByActionAndShop(action, shopId);
        try {
            return ResultJson.successResult(0, "success", count);
        } catch (Exception e) {
            return ResultJson.errorMes(e.getMessage());
        }
    }

    @RequestMapping("/countActionDate")
    public Object countActionDate(Integer shopId,String action,String beginTime,String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date begin = sdf.parse(beginTime);
            Date end = sdf.parse(endTime);
            long count = reportService.countByActionAndShop(action, shopId,begin,end);
            return ResultJson.successResult(0, "success", count);
        } catch (Exception e) {
            return ResultJson.errorMes(e.getMessage());
        }
    }

    @RequestMapping("/ad/countAction")
    public Object adCountAction(String action,Integer advertisePutId) {
        long count = reportService.countByActionAndAd(action, advertisePutId);
        try {
            return ResultJson.successResult(0, "success", count);
        } catch (Exception e) {
            return ResultJson.errorMes(e.getMessage());
        }
    }

    @RequestMapping("/ad/countActionDate")
    public Object adCountActionDate(String action,Integer advertisePutId,String beginTime,String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date begin = sdf.parse(beginTime);
            Date end = sdf.parse(endTime);
            long count = reportService.countByActionAndAd(action, advertisePutId,begin,end);
            return ResultJson.successResult(0, "success", count);
        } catch (Exception e) {
            return ResultJson.errorMes(e.getMessage());
        }
    }

    @RequestMapping("/ad/dayHistogram")
    public Object adDayHistogram(Integer advertisePutId,String beginTime,String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date begin = sdf.parse(beginTime);
            Date end = sdf.parse(endTime);
            List<Map<String,Object>> result = reportService.getAdDayHistogram(begin,end,advertisePutId);
            return ResultJson.successResult(0, "success", result);
        } catch (Exception e) {
            return ResultJson.errorMes(e.getMessage());
        }
    }

}
